import { _decorator, Component, EditBox, Label, Node, PageView, ProgressBar, ScrollView, Slider, Sprite, Toggle, UITransform } from 'cc';
const { ccclass, property } = _decorator;

//专门用于存储需要关注的节点和组件
//Map容器
//应该有添加接口
@ccclass('UIContianer')
export class UIContianer{
    //存储需要关注的节点容器
    private _mapNode:Map<string,Node> = new Map();
    //需要关注的组件，罗列出常用的组件，以后如果需要自己再补。
    private _mapLabel:Map<string,Label> = new Map();
    private _mapSprite:Map<string,Sprite> = new Map();
    private _mapProgress:Map<string,ProgressBar> = new Map();
    private _mapEditBox:Map<string,EditBox> = new Map();
    private _mapSlider:Map<string,Slider> = new Map();
    private _mapPageView:Map<string,PageView> = new Map();
    private _mapScrollView:Map<string,ScrollView> = new Map();
    private _mapToggle:Map<string,Toggle> = new Map();
    private _mapUITransform:Map<string,UITransform> = new Map();
    private _objMap:any = {
        Label:this._mapLabel,
        Sprite:this._mapSprite,
        ProgressBar:this._mapProgress,
        EditBox:this._mapEditBox,
        Slider:this._mapSlider,
        PageView:this._mapPageView,
        ScrollView:this._mapScrollView,
        Toggle:this._mapToggle,
        UITransform:this._mapUITransform
    }
    //添加接口接口
    addNode(nodeName:string,node:Node){
        if(nodeName.length <=0||this._mapNode.has(nodeName)||!node){
            return;
        }
        this._mapNode.set(nodeName,node);
    }
    //添加组件接口
    //节点名字， :区分某种组件（比如Label里面某个Label）
    //组件是什么组件。 字符串。    'Label'
    //组件
    addComp(nodeName:string,compName:string,comp:Component){
        //判断
        if(nodeName.length <=0||compName.length <=0||!comp){
            return;
        }
        //通过组件名字去找到存储这种组件的Map容器
        let mapComp = this._objMap[compName];
        //如果容器获取不到，那么什么都不处理
        if(!mapComp){
            return;
        }
        //存储的组件名字，以节点名字
        mapComp.set(nodeName,comp);
    }
    //获取接口。
    getNode(nodeName:string){
        if(nodeName.length <=0){
            return null;
        }
        return this._mapNode.get(nodeName);
    }
    //获取组件接口
    //节点名字，组件类型
    getComp<T>(nodeName:string,compName:string):T{
        if(nodeName.length <=0||compName.length <=0){
            return null;
        }
        //通过组件类型名字拿到map容器
        let mapComp = this._objMap[compName];
        if(!mapComp){
            return null;
        }
        return mapComp.get(nodeName) as T;
    }
}


